package com.yxy.springcloud.framework.unionauth.controller;


import com.yxy.springcloud.framework.common.base.BusinessException;
import com.yxy.springcloud.framework.common.base.RestResponse;
import com.yxy.springcloud.framework.unionauth.DTO.AppLoginDto;
import com.yxy.springcloud.framework.unionauth.DTO.ChangePasswordDto;
import com.yxy.springcloud.framework.unionauth.model.AdminUser;
import com.yxy.springcloud.framework.unionauth.service.AppLoginService;
import com.yxy.springcloud.framework.unionauth.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * app 用户登录逻辑
 */
@RestController
@Slf4j
@RequestMapping("/app-login")
public class AppLoginController {

  @Autowired
  private AppLoginService loginService;

  //登录
  @PostMapping("/in")
  public RestResponse in(@RequestBody AdminUser loginUser, @RequestParam("appName") String appName) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "登陆成功");
    restResponse.setContent(loginService.in(loginUser, appName));
    return restResponse;
  }

  //登录with 验证码
  @PostMapping("/loginWithCheckCode")
  public RestResponse loginWithCheckCode(@RequestBody AppLoginDto loginUser, @RequestParam("appName") String appName) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "登陆成功");
    restResponse.setContent(loginService.loginWithCheckCode(loginUser, appName));
    return restResponse;
  }

  @GetMapping("/out")
  public RestResponse out() throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "登出成功");
    loginService.out();
    return restResponse;
  }

  @PostMapping("/update")
  public RestResponse update(@RequestBody AdminUser inputDto) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "信息修改成功");
    loginService.update(inputDto);
    return restResponse;
  }

  @GetMapping("/userInfo")
  public RestResponse userInfo(@RequestParam("appName") String appName) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "查询成功");
    restResponse.setContent(loginService.userInfo(appName));
    return restResponse;
  }

  // 用户自己修改 自己的登录密码
  @PostMapping("/changePassword")
  public RestResponse changePassword(@RequestBody ChangePasswordDto inputDto) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "密码修改成功");
    loginService.changePassword(inputDto);
    return restResponse;
  }
}
